<template>
  <!--关系实体-->
  <basic-container>
    <div>
      <m-s-search @searchClick="v=>{queryForm=v,page.currentPage=1,getList()}" :list="EntityRelationOption.column"></m-s-search>
    </div>
    <avue-crud :option="EntityRelationOption"
               :table-loading="loading"
               :data="data"
               @selection-change="v=>selection=v"
               @current-change="currentChange"
               @size-change="sizeChange"
               ref="crud"
               :page.sync="page">
      <template slot="menuLeft" slot-scope="{size}">
        <el-button :size="size" type="primary"
                   @click="addToTabsList('关系实体',{oid:-1},'modelRelationTemp','open')">
          创建
        </el-button>
        <el-button :size="size" type="danger" :disabled="selection.length===0" @click="remove(selection)">删除
        </el-button>
        <el-button :size="size" :disabled="selection.length!==1" @click="setIssue(selection)">发布
        </el-button>
        <el-button :size="size" :disabled="selection.length===0" @click="setCancellation(selection)">
          作废
        </el-button>
      </template>

      <template slot="nameCn" slot-scope="{row}">
                 <span class="column-btn" @click="addToTabsList(row.nameCn+'关系实体',row,'modelRelationTemp','view')">
                  {{ row.nameCn }}
                </span>
      </template>

      <template slot="menu" slot-scope="{row}">
        <el-button type="text" @click="addToTabsList(row.nameCn+'关系实体',row,'modelRelationTemp','view')">查看详情
        </el-button>
        <el-button :disabled="row.state===2?true:row.state===1" type="text"
                   @click="addToTabsList(row.nameCn+'关系实体',{...row, ...{btn:'jc'}},'modelRelationTemp','edit')">
          修改
        </el-button>
        <el-button :disabled="row.state===2?true:(row.state!==1||row.newFlag!==1)" type="text"
                   @click="addToTabsList(row.nameCn+'关系实体',{...row, ...{btn:'xd'}},'modelRelationTemp','edit')">
          修订
        </el-button>
        <el-button :disabled="row.state===2?true:row.checkState===1" type="text" @click="checkin(row)">
          检入
        </el-button>
        <el-button :disabled="row.state===2?true:row.checkState===1" type="text" @click="revokeCheckout(row)">
          撤销检出
        </el-button>
<!--        <el-button type="text">
          历史
        </el-button>-->
      </template>
    </avue-crud>

    <common-entity-dialog ref="ced"></common-entity-dialog>
    <ms-drawer @refresh="getList()" :visible.sync="visible" :tab-list.sync="tabsList"></ms-drawer>
  </basic-container>
</template>
<script>
import MSSearch from "@/DMDComponents/MSSearch.vue";
import CommonEntityDialog from "@/views/DMDComponent/commonEntityDialog.vue";

import * as api from "@/api/modulManage/relation"
import * as common from "@/api/common";
import * as relation from "@/api/modulManage/relation";

export default {
  name: "EntityRelation",
  components: {CommonEntityDialog, MSSearch},
  provide() {
    return {
      temp: "2",
      parentIdList: () => this.parentIdList,
      modelTypeList: () => this.modelTypeList
    }
  },
  inject: ["EntityRelationOption"],
  data() {
    return {
      queryForm: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 1
      },
      data: [],
      loading: false,
      selection: [],

      parentIdList: [],
      modelTypeList: [],

      visible: false,
      tabsList: [],
    }
  },
  methods: {
    revokeCheckout(row) {
      this.$confirm("是否撤销检出").then(() => {
        api.revokeCheckout({id: row.oid}).then(() => {
          this.$message.success("撤销检出成功");
          this.getList();
        })
      })
    },
    checkin(row) {
      this.$confirm("是否检入").then(() => {
        api.checkin({id: row.oid}).then(() => {
          this.$message.success("检入成功")
          this.getList();
        })
      })
    },
    // 向抽屉中添加tab页 根据 oid 去重
    addToTabsList(title, row, component, type) {
      let ids = this.tabsList.map(item => (item.row.oid + "-" + item.component));
      if (!ids.includes(row.oid + "-" + component)) {
        this.tabsList.push({title, row, component, type})
      }
      this.visible = true;
    },

    remove(data) {
      let ids = data.map(item => item.oid);
      this.$confirm("是否删除？").then(() => {
        api.del({ids: ids.join(",")}).then(() => {
          this.$message.success("删除成功");
          this.getList();
          this.selection = [];
        })
      })
    },
    setCancellation(data) {
      let ids = data.map(item => item.oid);
      this.$confirm("是否作废？").then(() => {
        api.cancellation({id: ids.join(",")}).then(() => {
          this.$message.success("作废成功");
          this.getList();
          this.selection = [];
        })
      })
    },
    setIssue(data) {
      let ids = data.map(item => item.oid);
      this.$confirm("是否发布？").then(() => {
        api.issue({id: ids.join(",")}).then(() => {
          this.$message.success("发布成功");
          this.getList();
          this.selection = [];
        })
      })
    },
    getList() {
      this.loading = true;
      api.getList({current: this.page.currentPage, size: this.page.pageSize, ...this.queryForm}).then(res => {
        const {list, totalRows} = res.data.data;
        this.data = list;
        this.page.total = totalRows;
        this.loading = false

        this.getEntityList();
      })
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList();

    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList();
    },


    // 获取 父模型
    getEntityList() {
      api.getFatherModules().then(res => {
        this.parentIdList = res.data.data;
      })
    },
    //获取 模型类型
    getModelType() {
      common.getDic("modelType").then(res => {
         if(!res.data.data){
          return;
        }
        const defaultFlag = localStorage.getItem("defaultFlag")
        if(defaultFlag === '1'){
          this.modelTypeList = res.data.data;
        }else{
          this.modelTypeList = res.data.data.filter(v=>v.value === '1');
        }
      })
    },
    getFromTo(){
      relation.from_to().then((res => {
        this.fromToTree = res.data.data;
      }))
    }
  },
  mounted() {
    this.getList();
    this.getEntityList();
    this.getModelType();
  }
}
</script>
